package com.caocaog.market.util;


import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.caocaog.market.vo.WxMiniBO;
import com.caocaog.market.vo.WxPhoneVO;
import org.apache.tomcat.util.codec.binary.Base64;
import org.jeecg.common.exception.JeecgBootException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

public class WxUtil {

    //微信code获取openid, unionid, session_key地址(松果)
    private static final String url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code"
            .replace("APPID", "wx45e8fed752921170")
            .replace("SECRET", "f0734668431266e9d950100c0b860c29");

    /**
     * 获取用户信息
     *
     * @param bo
     * @return
     */
    public static WxPhoneVO getUserInfo(WxMiniBO bo) {
        String res = HttpUtil.get(url.replace("JSCODE", bo.getCode()));
        JSONObject jsonObject = JSON.parseObject(res);
        String openid = jsonObject.getString("openid");
        String unionid = jsonObject.getString("unionid");
        String session_key = jsonObject.getString("session_key");
        System.out.println("------jsonObject----" + jsonObject);
        if (StrUtil.isEmpty(openid)) {
            throw new JeecgBootException("授权失败");
        }
        WxPhoneVO wxPhoneVO = new WxPhoneVO();
        if (null != bo.getData() && !bo.getData().isEmpty()) {
            String decrypt = WxUtil.decrypt(bo.getData(), session_key, bo.getIv());
             wxPhoneVO = JSONObject.parseObject(decrypt, WxPhoneVO.class);
            if (wxPhoneVO == null) {
                throw new JeecgBootException("获取用户信息失败");
            }

        }
        wxPhoneVO.setUnionId(unionid);
        wxPhoneVO.setOpenId(openid);
        wxPhoneVO.setSessionKey(session_key);
        return wxPhoneVO;

    }


    public static String decrypt(String data, String key, String iv) {
        //被加密的数据
        byte[] dataByte = Base64.decodeBase64(data);
        //加密秘钥
        byte[] keyByte = Base64.decodeBase64(key);
        //偏移量
        byte[] ivByte = Base64.decodeBase64(iv);
        try {
            AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivByte);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            return new String(cipher.doFinal(dataByte), StandardCharsets.UTF_8);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }


}
